package com.example.identifyvisio.service;


import com.example.identifyvisio.enumeration.LaneType;
import com.example.identifyvisio.model.R;
import com.example.identifyvisio.model.component.Position;
import com.example.identifyvisio.model.component.Size;
import com.example.identifyvisio.model.lane.Lane;
import com.example.identifyvisio.model.lane.LaneData;
import com.example.identifyvisio.model.lane.PositionAndLane;
import com.example.identifyvisio.model.lane.PositioningBasis;

import java.text.DecimalFormat;
import java.util.*;

import static com.example.identifyvisio.utis.NodeValueUtil.getRelationship;


//获取泳道元素
public class LaneService {


    /**
     * 获取泳道元素
     *
     * @param
     * @param
     * @return
     */
    public static R<PositionAndLane> getLane(HashMap<Integer, Map<String, String>> laneMap,
                                          HashMap<Integer, Map<String, String>> dataMap, Integer shapeID, Map<String, String> shapeMap,
                                          boolean level, PositioningBasis basis) {

        R<PositionAndLane> positionAndLaneR = new R<>();
        double width = 0;
        double height = 0;
        Size size = new Size();
        if (level) {
            width = 277 / 2;
            height = 33/ 2;
            size.setSize("水平泳道");
        } else {
            width = 45/ 2;
            height = 175 / 2;
            size.setSize("垂直泳道");
        }
        if (shapeMap.containsKey("Width")) {
            width = Double.parseDouble(shapeMap.get("Width")) / 2;
        }
        if (shapeMap.containsKey("Height")) {
            height = Double.parseDouble(shapeMap.get("Height")) / 2;
        }
        Lane lane = new Lane();
        if (basis.getLaneSum() > 0) {
            basis.setPinX(basis.getPinX() + 340);
        } else {
            double pinX1 = Double.parseDouble(shapeMap.get("PinX"));
            //得到泳道左上角坐标
            basis.setPinX(pinX1 - width);
            double pinY1 = Double.parseDouble(shapeMap.get("PinY"));
            //会话固定泳道Y在-250，所以这里+250获取visio泳道Y坐标与-250坐标的高度差。
            basis.setPinY(pinY1 + 250 + height);
        }

        Position position = new Position(
                basis.getPinX(),
                -250
        );
        ArrayList<Position> positions = new ArrayList<>(2);
        Position position2 = new Position(
                Double.parseDouble(shapeMap.get("PinX")) - width,
                Double.parseDouble(shapeMap.get("PinY")) + height
        );
        Position position3 = new Position(
                basis.getPinX(),
                basis.getPinY()
                );

        //缩放倍率

        double Y = size.getHeight() / height / 2 ;
        double X = size.getWidth() / width / 2 ;
        Position position1 = new Position(X, Y);

        positions.add(position3);
        positions.add(position2);
        positions.add(position1);

        HashMap<String, List<Position>> positionHashMap = basis.getPositionHashMap();
        positionHashMap.put(String.valueOf(shapeID), positions);

        basis.setPositionHashMap(positionHashMap);

        lane.setPosition(position);
        lane.setSize(size);
        lane.setView("react-shape-view");
        lane.setShape("react-shape");
        String uniqueID = shapeMap.get("UniqueID");
        lane.setId(uniqueID);

        String type = level?LaneType.LANEPRO.getErrMessage():LaneType.LANE.getErrMessage();
        getLaneData(shapeMap, type, lane);

        lane.setZIndex("-10");
        String s = shapeMap.get("relationships");
        if ("".equals(s)){
            positionAndLaneR.setMsg("流程图中无泳道，默认自定义一个水平泳道");
        }else {
            List<String> matches = getRelationship(shapeMap.get("relationships"));
            ArrayList<String> arrayList = new ArrayList<>();
            for (String match : matches) {
                if (dataMap.containsKey(Integer.valueOf(match))) {
                    Map<String, String> stringMap = dataMap.get(Integer.valueOf(match));
                    arrayList.add(stringMap.get("UniqueID"));
                }
            }
            lane.setChildren(arrayList);
        }

        lane.setOriginSize(size);
        lane.setOriginPosition(position);
        basis.setLaneSum(basis.getLaneSum() + 1);

        positionAndLaneR.setData(new PositionAndLane(lane,basis));

        return positionAndLaneR;
    }

    /**
     * 获取泳道datd数据
     * @param shapeMap
     * @param type
     * @param lane
     */
    private static void getLaneData(Map<String, String> shapeMap, String type, Lane lane) {

        String text = shapeMap.get("text");
        String aacName = text;
        String aacCode = null;
        if (text != null) {
            for (int i = 0; i < text.length(); i++) {
                char head = text.charAt(i);
                char tail = text.charAt(text.length() - 1);
                if (head == '(' && tail == ')' || head == '（' && tail == '）') {
                    aacName = text.substring(0, i);
                    aacCode = text.substring(i + 1, text.length() - 1);
                }
            }
        }
            LaneData laneData = new LaneData(
                    shapeMap.get("stateId"),
                    type,
                    true,
                    type,
                    aacName,
                    aacCode
            );
            lane.setData(laneData);
    }
}
